//
//  ByLARViewModel.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/26.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByLARViewModel.h"

#import "ByNetUserApi.h"    // 用户接口
#import "ByNetCodeApi.h"    // 验证码
@implementation ByLARViewModel

/** 登陆 */
- (void)login:(ByNetParamsModel *)params {
    
    NSLog(@"%@", params);
    if ([params.username isStringNull] ||
        [params.password isStringNull] ||
        [params.role isStringNull]) {
        [ByCoreToolAlert alertError:@"请填写完整注册信息"];
        return;
    }
    
    void (^failure)(ByNetBaseResult *result) = nil;
    
    if([self.loginDelegate respondsToSelector:@selector(loginFailure:)]) {
        
        failure = ^(ByNetBaseResult *result){
            [self.loginDelegate loginFailure:result];
        };
    }

    [ByNetUserApi login:params success:^(ByNetBaseResult *result) {
        if (self.loginDelegate) {
            [self.loginDelegate loginSuccess:result];
        }
    } failure:failure];
}

/** 注册 */
- (void)registe:(ByNetParamsModel *)params {
    NSLog(@"%@", params);
    if ([params.username isStringNull] ||
        [params.password isStringNull] ||
        [params.passwordConfirm isStringNull] ||
        [params.code isStringNull] ||
        [params.role isStringNull]) {
        [ByCoreToolAlert alertError:@"请填写完整注册信息"];
        return;
    }
    if (![params.password isEqualToString:params.passwordConfirm]) {
        [ByCoreToolAlert alertError:@"两次密码输入不同!"];
        return;
    }
    
    
    void (^failure)(ByNetBaseResult *result) = nil;
    
    if([self.registerDelegate respondsToSelector:@selector(registerFailure:)]) {
        
        failure = ^(ByNetBaseResult *result){
            [self.registerDelegate registerFailure:result];
        };
    }
    
    [ByNetUserApi reigster:params success:^(ByNetBaseResult *result) {
        if (self.registerDelegate) {
            [self.registerDelegate registerSuccess:result];
        }
    } failure:failure];
}

/** 获取验证码 */
- (void)getCode:(ByNetParamsModel *)params {
    
    char firstC = [params.mobile characterAtIndex:0];
    if (params.mobile.length < 6 || params.mobile.length > 32) {
        [ByCoreToolAlert alertError:@"用户名长度必须在6-32位"];
        return;
    }
    if (!((firstC >= 'A' && firstC <= 'Z') || (firstC >= 'a' && firstC <= 'z'))) {
        [ByCoreToolAlert alertError:@"用户名必须以英文字母开头"];
        return;
    }
    
    void (^failure)(ByNetBaseResult *result) = nil;
    
    if([self.registerDelegate respondsToSelector:@selector(getCodeFailure:)]) {
        
        failure = ^(ByNetBaseResult *result){
            [self.registerDelegate getCodeFailure:result];
        };
    }
    
    [ByNetCodeApi getCode:params success:^(ByNetBaseResult *result) {
        if (self.registerDelegate) {
            [self.registerDelegate getCodeSuccess:result];
        }
    } failure:failure];
}

@end
